package com.eims.sls_app.fragment;

import java.io.FileWriter;
import java.io.IOException;

import com.android.volley.toolbox.MySingleton;
import com.contans.view.GodFengshenVo;
import com.eims.sls_app.dataaccess.CircleBean;
import com.eims.sls_app.dataaccess.CircleBean.SchemeinfoBean.InformationIdBean;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.adapter.CircleFengshenAdapter;
import com.eims.sls_app.ui.adapter.CircleFengshenJiangjinAdapter;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.utils.BaseHelper;
import com.google.gson.Gson;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;

public class CircleFragment_cirFrament_fensheng_jiangjin extends Fragment {
	private String TAG = "CircleFragment_cirFrament_fensheng";
	private Activity activity;
	private ListView circle_item_listView;

	private String opt, auth, info, time, imei, crc; // 格式化后的参数

	private MyHandler mHandler;
	private MyAsynTask mMyAsynTask;
	private MySingleton mySingleton;
	private CircleBean circleBean;
	private InformationIdBean informationIdBean;
	private CircleFengshenJiangjinAdapter circleAdapter;
	private boolean isToFirst = true;
	private TextView circle_item_hint;
	private  GodFengshenVo fengshenVo;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_circle_zq_circle_fensheng, container, false);
		activity = getActivity();

		mHandler = new MyHandler();
		Log.e("11", "11");
		init(view);
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void init(View view) {
		mySingleton = MySingleton.getInstance(activity);
		circle_item_hint= (TextView) view.findViewById(R.id.circle_item_hint);
		circle_item_listView = (ListView) view.findViewById(R.id.listview);
		circle_item_listView.setEmptyView(circle_item_hint);
		info = "";
		MyAsynTask myAsynTask = new MyAsynTask();
		myAsynTask.execute();

		

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	class MyAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-1";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "95";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(getActivity());
			// info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);

			// String key = MD5.md5(AppTools.user.getUserPass() + AppTools.MD5_key);
			String key = App.key;
			if (App.user == null) {
				crc = RspBodyBaseBean.getCrc(time, imei, key, info, "-1");
				auth = RspBodyBaseBean.getAuth(crc, time, imei, "-1");
			} else {
				crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
				auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			}

			String[] values = { opt, auth, info };

			String result = HttpUtils.doPost(App.names, values, App.path);
			
			System.out.println("晒单：" + result);
		
			if ("-500".equals(result)) {
				return "-500";
			}

			if (result!=null) {
				
				Gson gson=new Gson();
			    fengshenVo=gson.fromJson(result, GodFengshenVo.class);
//				Log.i(TAG, "晒单" + fengshenVo.toString());
				
			}
			
			error = "0";
			return error;
		}

		@Override
		protected void onPostExecute(String result) {

			mHandler.sendEmptyMessage(Integer.parseInt(result));

			super.onPostExecute(result);
		}
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
		
			ProgressDialogBar.stopProgress();
			switch (msg.what) {
			case 0:

				if (fengshenVo != null) {
					circleAdapter = new CircleFengshenJiangjinAdapter(getActivity(),fengshenVo.getJjb());
					circle_item_listView.setAdapter(circleAdapter);
				}

				// MyToast.getToast(context, "晒单成功").show();
				break;
			case 1:

				// MyToast.getToast(context, "重复晒单").show();
				break;
			case -1:

				break;
			case -500:

				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	}

	
}
